using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using UnityEngine;

namespace HuaXianQu.ProtoBuffEx.ToolEditor
{
    public class ExcelToProtoFile
        {
            public List<IProtoType> TableParses;
            public List<string> ImportList = new List<string>();
            public List<string> CustomClassList = new List<string>();
            public string ProtoName;
            public string FileName;
            public string ProtoBuffFilePath;
            private string _protoBuffRootPath;
            private bool _isDataClass = false;
            

            public void Init(string protoName, string protoBuffFilePath, string dirRootPath,
                List<IProtoType> tableParses = null)
            {
                FileName=protoName;
                Match match= Regex.Match(protoName, @"([\w]+)@*([\w\W]*)");
                if (match.Groups.Count == 3)
                {
                    ProtoName=match.Groups[1].Value;
                }
                // Debug.LogError($"=========={ProtoName}");
                ProtoBuffFilePath = $"{protoBuffFilePath}/{ProtoName}.proto";
                this._protoBuffRootPath = dirRootPath;
                TableParses = tableParses;
                HashSet<string> customClassList = new HashSet<string>();
                List<string> importList = new List<string>();
                if (TableParses == null)
                {
                    TableParses = new List<IProtoType>();
                    return;
                }

                foreach (var info in TableParses)
                {
                    if (info.IsDataClass)
                    {
                        _isDataClass = true;
                    }

                    foreach (var variable in info.CurObjectInfo.CustomVariableNameList)
                    {
                        CustomClassList.Add(variable);
                        customClassList.Add(variable);
                    }

                    foreach (var variable in info.CurObjectInfo.ReferenceOfVariableNameList)
                    {
                        if (string.IsNullOrWhiteSpace(variable))
                        {
                            continue;
                        }

                        if (!importList.Contains(variable))
                        {
                            importList.Add(variable);
                        }
                    }
                }

                foreach (var item in importList)
                {
                    if (!customClassList.Contains(item))
                    {
                        ImportList.Add(item);
                    }
                }
            }

            public void ExcelToProtoBuffFlie(string excelVariableAtttibutePath, List<string> importList)
            {
                string protoStr = $"syntax = \"proto3\";\n";
                if (_isDataClass)
                {
                    protoStr += $"{excelVariableAtttibutePath}\n";
                }

                if (importList.Count > 0)
                {
                    protoStr += $"import \"{string.Join("\";\nimport \"", importList)}\";\n";
                }

                protoStr += string.Join('\n', TableParses);

                using (StreamWriter streamWriter =
                       File.CreateText($"{_protoBuffRootPath}/{ProtoBuffFilePath}"))
                {
                    streamWriter.Write(protoStr);
                    streamWriter.Close();
                }
            }
        }
}